/*
 * Created on Mar 3, 2010
 */
package com.mgmaps.utils;

import javax.microedition.lcdui.*;
import javax.microedition.midlet.MIDletStateChangeException;
import com.mgmaps.app.APPCanvas;
import com.mgmaps.app.APPCommands;
import com.mgmaps.app.APPMidlet;
import com.mgmaps.tools.StringTools;
import com.mgmaps.utils.ExtLogger;
import com.nutiteq.log.Log;

public class ExtLogger {
  
  private static final Form fatalForm = new Form("Fatal Error");
  private static final StringItem fatalItem = new StringItem("", "");
  private static final Command cmdExit = new Command("Exit", Command.EXIT, 101);

  private static boolean initialized = false;
  
  /**
   * Class ExitListener - called when the program exits.
   */
  static class ExitListener implements CommandListener {
    /**
     * Called when exit is selected.
     * @param command
     * @param current
     * @see javax.microedition.lcdui.CommandListener#commandAction(javax.microedition.lcdui.Command, javax.microedition.lcdui.Displayable)
     */
    public void commandAction(Command command, Displayable current) {
      if (APPCanvas.instance != null)
        APPCanvas.instance.commandAction(APPCommands.instance.exitCommand, current);
      else
        try {
          APPMidlet.instance.destroyApp(true);
        }
        catch (MIDletStateChangeException ignore) {
        }
    }
  }
  
  private ExtLogger() {
  }
  
  /**
   * Initialize the logger.
   */
  public static void initializeLog() {
    if (initialized)
      return;
    
    Log.enableAll();
    
    // build the form
    fatalForm.append(fatalItem);
    fatalForm.addCommand(cmdExit);
    fatalForm.setCommandListener(new ExtLogger.ExitListener());
    
    initialized = true;
  }
  
  
  public static void debug(String msg) {
    Log.debug(msg);
  }
  
  public static void error(String msg) {
    Log.error(msg);
  }
  
  public static void clear() {
    Log.clear();
  }
  
  
  /**
   * Handle an exception.
   * 
   * @param ex exception to handle
   * @param halt whether to halt program execution
   * @param description additional description about the location of the exception
   */
  public static void handle(Exception ex, boolean halt, String description) {
    ex.printStackTrace();
    
    // store the error message
    StringBuffer currentMessage = new StringBuffer();
    
    currentMessage.append(StringTools.timeFormat(System.currentTimeMillis()));
    if (halt)
      currentMessage.append(": FATAL ERROR: ");
    else
      currentMessage.append(": ERROR: ");
    currentMessage.append("EX: ");
    currentMessage.append(ex.getClass().getName());
    currentMessage.append(", DESC: ");
    currentMessage.append(description);
    currentMessage.append(", MSG: ");
    currentMessage.append(ex.getMessage());
    currentMessage.append('\n');
    
    String msg = currentMessage.toString();
    
    // debug
    // ex.printStackTrace();

    Log.printStackTrace(ex);
    Log.error(msg);
    
    // if a fatal exception occurs, display a window then quit 
    if (halt) {
      // display the error
      fatalItem.setText(Log.getLog());
      Display.getDisplay(APPMidlet.instance).setCurrent(fatalForm);
    }
  }
  
  public static String getLogString() {
    return Log.getLog();
  }
}
